﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Control : MonoBehaviour
{
    //按键对应面板
    public Button but1;
    public Button but2;
    public Button but3;
    public GameObject[] img = new GameObject[3];
    //三个角色
    public GameObject role1;
    public GameObject role2;
    public GameObject role3;
    Animator ani1;
    Animator ani2;
    Animator ani3;
    //计时器参数
    float time = 1;//倒计时时长
    int sign = 0;
    Button o1;
    Button o2;
    Button o3;
    Button s1;
    Button s2;
    Button s3;

    public int energy=20;//能量值
    public Text energyText;//能量值文本
    public GameObject barrier;//屏障面板
    //技能消耗能量值
    int consume1 = 1;
    int consume2 = 3;
    int consume3 = 1;
    int consume4 = 3;
    int consume5 = 1;
    int consume6 = 3;
    public int frequency = 3;//攻击次数

    GameObject finish;//战斗单例
    GameObject data;
    public GameObject victory;//胜利面板
    public GameObject fail;//失败面板
    Button again1;
    Button town1;
    Button again2;
    Button town2;
    // Start is called before the first frame update
    void Start()
    {
        data = GameObject.FindGameObjectWithTag("Data");
        
        finish = GameObject.FindGameObjectWithTag("Finish");
        role1 = GameObject.Find("Mori");
        role2 = GameObject.Find("OspreyCaptian");
        role3 = GameObject.Find("TraumaGuard");
        ani1 = role1.GetComponent<Animator>();
        ani2 = role2.GetComponent<Animator>();
        ani3 = role3.GetComponent<Animator>();
        o1 = img[0].transform.GetChild(0).GetComponent<Button>();
        o2 = img[1].transform.GetChild(0).GetComponent<Button>();
        o3 = img[2].transform.GetChild(0).GetComponent<Button>();
        s1 = img[0].transform.GetChild(1).GetComponent<Button>();
        s2 = img[1].transform.GetChild(1).GetComponent<Button>();
        s3 = img[2].transform.GetChild(1).GetComponent<Button>();
        energy = 10;
        energyText.text = "" + energy;
        again1 = victory.transform.GetChild(2).GetComponent<Button>();
        town1 = victory.transform.GetChild(3).GetComponent<Button>();
        again2 = victory.transform.GetChild(2).GetComponent<Button>();
        town2 = victory.transform.GetChild(3).GetComponent<Button>();
        Assignment();

    }

    public void EnergyUpdata(int i)
    {
        energy += i;
        energyText.text = "" + energy;
    }

    //为按键添加事件
    void Assignment()
    {
        o1.onClick.AddListener(
            delegate
            {
                Ordinary1(); 
            });
        o2.onClick.AddListener(
            delegate
            {
                Ordinary2();
            });
        o3.onClick.AddListener(
            delegate
            {
                Ordinary3();
            });
        s1.onClick.AddListener(
            delegate
            {
                Special1();
            });
        s2.onClick.AddListener(
            delegate
            {
                Special2();
            });
        s3.onClick.AddListener(
            delegate
            {
                Special3();
            });
        but1.onClick.AddListener(
            delegate
            {
                ImgBut(0);
            });
        but2.onClick.AddListener(
            delegate
            {
                ImgBut(1);
            });
        but3.onClick.AddListener(
            delegate
            {
                ImgBut(2);
            });
        again1.onClick.AddListener(
            delegate
            {
                AgainScence();
            });
        town1.onClick.AddListener(
            delegate
            {
                TownScence();
            });
        again2.onClick.AddListener(
            delegate
            {
                AgainScence();
            });
        town2.onClick.AddListener(
            delegate
            {
                TownScence();
            });

    }

    void AgainScence()
    {
        SceneManager.LoadScene("PlayGame"); 
    }

    void TownScence()
    {
        SceneManager.LoadScene("SampleScene");
    }

    //玩家回合
    public void PlayerRound()
    {
        frequency = 3;
        barrier.SetActive(false);
    }

    //玩家攻击次数
    void PlayerFrequency()
    {
        frequency--;
        if (frequency ==0)
        {
            StartCoroutine("Susped");

        }

    }

    IEnumerator Susped()
    {
        yield return new WaitForSeconds(1.5f);
        barrier.SetActive(true);
        if (finish.transform.childCount >0)
        {
            finish.GetComponent<BattleData>().EnemyAttack();
        }
        if (finish .transform .childCount ==0)
        {
            finish.GetComponent<BattleData>().Reward();
            victory.SetActive(true);
            int goldCoin = finish.GetComponent<BattleData>().goldCoin;
            victory.transform.GetChild(0).GetComponent<Text>().text = "" + goldCoin;
            int experience = finish.GetComponent<BattleData>().experience;
            victory.transform.GetChild(1).GetComponent<Text>().text = "" + experience;

        }
        StopCoroutine("Susped");
    }

    //枪战士特殊攻击
    void Special3()
    {
        if (energy >= consume6)
        {
            ani3.SetBool("skilltwo", true);
            sign = 5;
            time = 1;
            role3.GetComponent<RoleSkillsTwo>().Skills2();
            energy -= consume6;
            PlayerFrequency();
        }
        energyText.text = "" + energy;

    }

    //枪战士普攻
    void Ordinary3()
    {
        if (energy >= consume5)
        {
            ani3.SetBool("skillone", true);
            sign = 4;
            time = 1;
            role3.GetComponent<RoleSkillsTwo>().Skills1();
            energy -= consume5;
            PlayerFrequency();
        }
        energyText.text = "" + energy;

    }

    //斧战士特殊攻击
    void Special2()
    {
        if (energy >= consume4)
        {
            ani2.SetBool("skilltwo", true);
            sign = 3;
            time = 1;
            role2.GetComponent<RoleSkillThree>().Skills2();
            energy -= consume4;
            PlayerFrequency();
        }
        energyText.text = "" + energy;

    }

    //斧战士普攻
    void Ordinary2()
    {
        if (energy >= consume3)
        {
            ani2.SetBool("skillone", true);
            sign = 2;
            time = 1;
            role2.GetComponent<RoleSkillThree>().Skills1();
            energy -= consume3;
            PlayerFrequency();
        }
        energyText.text = "" + energy;

    }

    //法师特殊攻击
    void Special1()
    {
        if (energy >= consume2)
        {
            ani1.SetBool("skilltwo", true);
            sign = 1;
            time = 1;
            role1.GetComponent<RoleSkillsOne>().Skills2();
            energy -= consume2;
            PlayerFrequency();
        }
        energyText.text = "" + energy;

    }

    //法师普攻
    void Ordinary1()
    {
        if (energy >= consume1)
        {
            ani1.SetBool("skillone", true);
            sign = 0;
            time = 1;
            role1.GetComponent<RoleSkillsOne>().Skills1();
            energy -= consume1;
            PlayerFrequency();
        }
        energyText.text = "" + energy;

    }

    //转换面板
    void ImgBut(int n)
    {
        for (int i=0;i<img .Length;i++)
        {
            if (i==n)
            {
                img[i].SetActive(true);
            }
            else
            {
                img[i].SetActive(false);
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (time>0)
        {
            time -= Time.deltaTime;
        }
        if (time <0)
        {
            Classification();
        }
    }

    //通过寻找索引找到对应的动画
    void Classification()
    {
        switch (sign)
        {
            case 0: ani1.SetBool("skillone", false);break;
            case 1: ani1.SetBool("skilltwo", false); break;
            case 2: ani2.SetBool("skillone", false); break;
            case 3: ani2.SetBool("skilltwo", false); break;
            case 4: ani3.SetBool("skillone", false); break;
            case 5: ani3.SetBool("skilltwo", false); break;

        }
    }

}
